﻿Imports System.Windows.Forms
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class frmImportBackImage
    ''' <summary>
    ''' 背景图的集合
    ''' </summary>
    ''' <remarks></remarks>
    Private m_InsetImageCollection As New List(Of CImPortImage.InsertImage)
    ''' <summary>
    ''' 背景图的集合
    ''' </summary>
    ''' <remarks></remarks>
    Property InsetImageCollection() As List(Of CImPortImage.InsertImage)
        Get
            Return Me.m_InsetImageCollection
        End Get
        Set(ByVal value As List(Of CImPortImage.InsertImage))
            Me.m_InsetImageCollection = value
        End Set
    End Property

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click

        Cal()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    ''' <summary>
    ''' 导入地图
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdImports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdImports.Click
        Dim frmDialog As New OpenFileDialog
        frmDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
        If frmDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.m_InsetImageCollection.Add(New CImPortImage.InsertImage) '添加一个新的背景图实例
            Me.ImportImageControl1.InsertImage = Me.m_InsetImageCollection(Me.m_InsetImageCollection.Count - 1)
            Me.ImportImageControl1.InsertImage.FileName = frmDialog.FileName
            Me.ImportImageControl1.InsertImage.LoadBitmap(frmDialog.FileName)
            Me.ImportImageControl1.InsertImage.FileNameNoExen = CImPortImage.InsertImage.GetFileName(frmDialog.FileName)
            ' Me.Panel3.Visible = True
            Me.ListBox1.Items.Add(Me.ImportImageControl1.InsertImage.FileNameNoExen)
            Me.ListBox1.SelectedIndex = Me.ListBox1.Items.Count - 1
            Me.Refresh()

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.ImportImageControl1.SetMouseType(1) '箭头形
        Me.ImportImageControl1.MouseMoveType = 1 '移动
        Me.ImportImageControl1.SetMouseType(1)
        '定位第2个点
        Me.ImportImageControl1.SetPoint = 2
        Me.Refresh()
    End Sub

    Private Sub 鼠标定位_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 鼠标定位.Click
        Me.ImportImageControl1.SetMouseType(1) '箭头形
        Me.ImportImageControl1.MouseMoveType = 1 '移动
        Me.ImportImageControl1.SetMouseType(1)
        '定位第一个点
        Me.ImportImageControl1.SetPoint = 1
        Me.Refresh()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        Me.ImportImageControl1.InsertImage.DrawImage(e.Graphics)
    End Sub



    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.ImportImageControl1.InsertImage.OriginX = Me.ImportImageControl1.InsertImage.OriginX - 10
        Me.ImportImageControl1.InsertImage.OriginY = Me.ImportImageControl1.InsertImage.OriginY - 10
        Me.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.ImportImageControl1.InsertImage.gScale = Me.ImportImageControl1.InsertImage.gScale / 2
    End Sub

    Private Sub ToolStripButtonZoomIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonZoomIn.Click
        Me.ImportImageControl1.SetMouseType(0) '箭头形
        Me.ImportImageControl1.MouseMoveType = 2 '放大
        Me.ImportImageControl1.SetPoint = 0
        Me.ImportImageControl1.SetMouseType(2) '箭头形
    End Sub

    Private Sub ToolStripButtonZoomOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonZoomOut.Click
        Me.ImportImageControl1.SetMouseType(0) '箭头形
        Me.ImportImageControl1.MouseMoveType = 3 '缩小
        Me.ImportImageControl1.SetPoint = 0
        Me.ImportImageControl1.SetMouseType(3) '箭头形
    End Sub

    Private Sub ToolStripButtonMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonMove.Click
        Me.ImportImageControl1.SetMouseType(0) '箭头形
        Me.ImportImageControl1.MouseMoveType = 4 '移动
        Me.ImportImageControl1.SetPoint = 0
        '移动
        Me.ImportImageControl1.SetMouseType(4)
    End Sub

    Private Sub ImportImageControl1_InsertImageChanged(ByRef InsertImageTemp As CImPortImage.InsertImage) Handles ImportImageControl1.InsertImageChanged
        If Me.ListBox1.SelectedIndex >= 0 Then
            Me.m_InsetImageCollection(Me.ListBox1.SelectedIndex) = InsertImageTemp
        End If
    End Sub

    Private Sub ImportImageControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportImageControl1.Load

    End Sub

    Private Sub ToolStripButtonArrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.ImportImageControl1.SetMouseType(0) '箭头形
        Me.ImportImageControl1.MouseMoveType = 0 '移动
        Me.ImportImageControl1.SetMouseType(0)
    End Sub

    Private Sub frmImportBackImage1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Me.m_InsetImageCollection.Count > 0 Then
            For i As Integer = 0 To Me.m_InsetImageCollection.Count - 1
                Me.ListBox1.Items.Add(Me.m_InsetImageCollection(i).FileNameNoExen)
            Next
            Me.ListBox1.SelectedIndex = 0
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If Me.ListBox1.SelectedIndex >= 0 Then
            Me.ImportImageControl1.InsertImage = Me.m_InsetImageCollection(Me.ListBox1.SelectedIndex).Clone
            Me.txtX1.Text = Me.ImportImageControl1.InsertImage.PointReal1.X
            Me.txtY1.Text = Me.ImportImageControl1.InsertImage.PointReal1.Y

            Me.txtX2.Text = Me.ImportImageControl1.InsertImage.PointReal2.X
            Me.txtY2.Text = Me.ImportImageControl1.InsertImage.PointReal2.Y
            Me.Refresh()
        End If
    End Sub

   
    Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel.Click
        Dim i As Integer = Me.ListBox1.SelectedIndex
        If i >= 0 Then
            Me.m_InsetImageCollection.RemoveAt(i)
            Me.ListBox1.Items.RemoveAt(i)
            If i > Me.ListBox1.Items.Count - 1 Then
                i = i - 1
            End If
            If i < 0 Then
                i = -1
            End If
            Me.ListBox1.SelectedIndex = i
        End If

    End Sub

    Public Sub Cal()
        For i As Integer = 0 To Me.m_InsetImageCollection.Count - 1
            With Me.m_InsetImageCollection(i)
                If (.Point2.X - .Point1.X) * (txtX2.Text - txtX1.Text) <= 0 Or (.Point2.Y - .Point1.Y) * (txtY2.Text - txtY1.Text) <= 0 Then
                    MsgBox(.FileNameNoExen & "设置的坐标反向，请重新设置!", MsgBoxStyle.Information, "错误")
                Else
                    Dim LeftBottomPoint As Point
                    LeftBottomPoint.X = .PointReal2.X + (.BackRect.X - .Point2.X) * (.PointReal1.X - .PointReal2.X) / (.Point1.X - .Point2.X)
                    LeftBottomPoint.Y = .PointReal2.Y + (.BackRect.Y - .Point2.Y) * (.PointReal1.Y - .PointReal2.Y) / (.Point1.Y - .Point2.Y)
                    .LeftBottomPoint = LeftBottomPoint
                    Dim RightTopPoint As Point
                    RightTopPoint.X = .PointReal1.X + (.BackRect.X + .BackRect.Width - .Point1.X) * (txtX2.Text - .PointReal1.X) / (.Point2.X - .Point1.X)
                    RightTopPoint.Y = .PointReal1.Y + (.BackRect.Y + .BackRect.Height - .Point1.Y) * (txtY2.Text - .PointReal1.Y) / (.Point2.Y - .Point1.Y)
                    .RightTopPoint = RightTopPoint
                End If
            End With
        Next


    End Sub


    Private Sub txtY2_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtY2.Validating
        Dim PointR2 As Point
        PointR2.X = Me.txtX2.Text
        PointR2.Y = Me.txtY2.Text
        Me.ImportImageControl1.InsertImage.PointReal2 = PointR2
    End Sub

    Private Sub txtX2_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtX2.Validating
        Dim PointR2 As Point
        PointR2.X = Me.txtX2.Text
        PointR2.Y = Me.txtY2.Text
        Me.ImportImageControl1.InsertImage.PointReal2 = PointR2
    End Sub

    Private Sub txtX1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtX1.Validating
        Dim PointR1 As Point
        PointR1.X = Me.txtX1.Text
        PointR1.Y = Me.txtY1.Text
        Me.ImportImageControl1.InsertImage.PointReal1 = PointR1
    End Sub

    Private Sub txtY1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtY1.Validating
        Dim PointR1 As Point
        PointR1.X = Me.txtX1.Text
        PointR1.Y = Me.txtY1.Text
        Me.ImportImageControl1.InsertImage.PointReal1 = PointR1
    End Sub
End Class

